Fastify is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Fastify.
onSend
We can change the response payload being sent with the onSend
hook.
For example, we can write:
const fastify = require('fastify')({})
fastify.addHook('onSend', (request, reply, payload, done) => {
const err = null;
const newPayload = payload.replace('some-text', 'some-new-text')
done(err, newPayload)
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We replace the 'some-text'
response with 'some-new-text'
.
Then when we make a GET request to /
, we see 'some-new-text’
returned.
Equivalently, we can do the same with an async
function:
const fastify = require('fastify')({})
fastify.addHook('onSend', async (request, reply, payload) => {
const newPayload = payload.replace('some-text', 'some-new-text')
return newPayload
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We can also remove the response payload with:
const fastify = require('fastify')({})
fastify.addHook('onSend', (request, reply, payload, done) => {
reply.code(304)
const newPayload = null
done(null, newPayload)
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
onResponse
The onResponse
hook lets us run code when a response is sent.
It’s a useful place to gather statistics.
For instance, we can write:
const fastify = require('fastify')({})
fastify.addHook('onResponse', (request, reply, done) => {
console.log(reply)
done()
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to log the reply
.
We can also write:
const fastify = require('fastify')({})
fastify.addHook('onResponse', async (request, reply) => {
console.log(reply)
return
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to do the same thing.
onTimeout
The onTimeout
hook lets us monitor request timeouts.
For instance, we can use it by writing:
const fastify = require('fastify')({})
fastify.addHook('onTimeout', (request, reply, done) => {
console.log('timed out')
done()
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
or:
const fastify = require('fastify')({})
fastify.addHook('onTimeout', async (request, reply) => {
console.log('timed out')
return
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to log any request timeouts.
Managing Errors in Hooks
We can raise errors in hooks and return error responses if there are any errors.
For example, we can write:
const fastify = require('fastify')({})
fastify.addHook('preHandler', (request, reply, done) => {
reply.code(400)
done(new Error('Some error'))
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to add the preHandler
hook and return 400 errors.
We can also pass in an Error
instance in the done
function.
Also, we can write:
const fastify = require('fastify')({})
fastify.addHook('onResponse', async (request, reply) => {
throw new Error('Some error')
})
const start = async () => {
try {
fastify.get('/', function (request, reply) {
reply.send('some-text')
})
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to do the same thing with async
functions.
Conclusion
We can add various hooks with Fastify to handle events and errors.